package com.infinitegroup.findmystuff.activities;

import java.util.List;
import java.util.Map;

import com.infinitegroup.findmystuff.R;
import com.infinitegroup.findmystuff.presenters.AdminHomePresenter;
import com.infinitegroup.findmystuff.views.AdminHomeView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AdminHomeActivity extends Activity implements AdminHomeView{
	
	private AdminHomePresenter presenter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_adminhome);
		
		Bundle extras = getIntent().getExtras();
		String value = extras.getString("username"); // retrieve the username passed in by login.
		presenter = new AdminHomePresenter(this, value);		
		
		// Update the welcome message
		((TextView) findViewById(R.id.adminhome_welcome)).setText("Welcome " + presenter.getFirstName());
		
		// Get the locked accounts to display.
		List<Map<String, String>> lockedUserList = presenter.getLockedAccountsToDisplay();
		String itemString = generateLockedUserString(lockedUserList);
		((TextView) findViewById(R.id.adminhome_lockeduser_string)).setText(itemString);
		
		//Set up button listeners
		Button unlockUser = (Button) findViewById(R.id.adminhome_unlockbutton);
		Button deleteUser = (Button) findViewById(R.id.adminhome_deletebutton);
		Button registerAdmin = (Button) findViewById(R.id.adminhome_register_newadmin);
		
		unlockUser.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String username = ((EditText)findViewById(R.id.adminhome_username_text)).getText().toString();
				presenter.handleUnlockClick(username);
			}
		});
		deleteUser.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String username = ((EditText)findViewById(R.id.adminhome_username_text)).getText().toString();
				presenter.handleDeleteClick(username);
			}
		});
		registerAdmin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				presenter.handleRegisterClick();
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
	    if ((keyCode == KeyEvent.KEYCODE_BACK)){
	        finish();
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	
	public String generateLockedUserString(List<Map<String, String>> lockedUserList) {
		String result = "";
		if (lockedUserList!=null){
			for (Map<String,String> m : lockedUserList){
				result = result + "Name: " + m.get("username") + " | " + m.get("phone") + "\n" + "Email: " +m.get("email") + "\n";
			}
		}
		return result;
	}


	@Override
	public void displayMessage(String string) {
		Toast toast = Toast.makeText(this, string, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}


	@Override
	public void updateWelcome(String username) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void refreshString(List<Map<String,String>> list) {
		String newLocked = generateLockedUserString(list);
		((TextView) findViewById(R.id.adminhome_lockeduser_string)).setText(newLocked);		
	}


	@Override
	public void advanceToAdminRegistration(String username) {
		Intent intent = new Intent(this, AdminRegistrationActivity.class);
		//intent.putExtra("username", username);
		startActivity(intent);
	}

}
